﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MacViet.DataBaseAPI;

public partial class Control_Bill_UCBill_Add : System.Web.UI.UserControl
{  
    public DataTable dtResult = new DataTable();
    private string acc_email = string.Empty;
    private string b;
    private string[] array;
    private DateTime DateBill;
    PRODUCTSDataAPI product = new PRODUCTSDataAPI();


    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            //Date Time
            //----------------------------------------------------------------
            txtDate.Text = DateTime.Now.ToShortDateString();

            //Product_Types
            //----------------------------------------------------------------
            PRODUCT_TYPESDataAPI product_type = new PRODUCT_TYPESDataAPI();
            DataTable dt = product_type.getALL();
            ddlProTyp.DataSource = dt;
            ddlProTyp.DataTextField = "PROTYP_NAME";
            ddlProTyp.DataValueField = "PROTYP_ID";
            ddlProTyp.DataBind();
            loadProduct();
            if (Session["ACC_EMAIL"] != null)
                acc_email = Session["ACC_EMAIL"].ToString();
            btnCollect.Visible = false;


        }

    }
    protected void ddlProTyp_SelectedIndexChanged(object sender, EventArgs e)
    {

        int acc_id = 0;
        ACCOUNTSDataAPI account = new ACCOUNTSDataAPI();
        DataTable dt = account.getAccountByEmail("trunghieu61087@gmail.com");

        if (dt != null)
            acc_id = int.Parse(dt.Rows[0]["ACC_ID"].ToString());

        loadProduct();

    }

    protected void ddlProTyp_Load(object sender, EventArgs e)
    {
        // Mac dinh de lay gia tri dau tien.
        ddlProTyp.SelectedValue.ToString();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        btnCollect.Visible = true;
        try
        {
            int prodID = int.Parse(ddlPro.SelectedValue.ToString());
            string proName = string.Empty;
            string proDescription = string.Empty;
            int quantity = int.Parse(txtQuantity.Text);
            float price = 0;
            DataTable dts = product.getByPrimaryKeys(prodID);

            if (dts.Rows.Count > 0)
            {
                proDescription = dts.Rows[0]["PRO_DESCRIPTION"].ToString();
                price = float.Parse(dts.Rows[0]["PRO_PRICE"].ToString());
                proName = dts.Rows[0]["PRO_NAME"].ToString();
            }
            product.GoIntoCart(prodID, proName, proDescription, price, quantity);
            LoadData();
            string total = txtTotal.Text;

            if (txtTotal.Visible == true)
            {
                addMoneyToSub();
                MoneyWithTax();
                CheckChange();
                txtTax.Visible = true;
                txtTotal.Visible = true;
            }
            else
            {
                txtTax.Text = "0";
                addMoneyToSub();
            }

        }
        catch (Exception ex)
        {
            ex.ToString();
        }
        ////-------------------------------------------------------------------------------------------------

        //-------------------------------------------------------------------------------------------------


    }

    protected void btnCollect_Click(object sender, EventArgs e)
    {
        try
        {
            lbltax.Visible = true;
            txtTax.Visible = true;
            lbl.Visible = true;

            lblTotal.Visible = true;
            txtTotal.Visible = true;

            lblPayment.Visible = true;
            ddlPayment.Visible = true;

            lblCash.Visible = true;
            txtCash.Visible = true;

            lblChange.Visible = true;
            txtChange.Visible = true;


            LoadPayment();
            if (txtTax.Text == "0")

                txtTotal.Text = txtSub.Text;
        }
        catch (Exception ex)
        {
            ex.ToString();
        }

    }

    protected void txtTax_TextChanged(object sender, EventArgs e)
    {
        MoneyWithTax();
        txtCash.Text = "";
        txtChange.Text = "";
    }

    protected void txtCash_TextChanged(object sender, EventArgs e)
    {
        CheckChange();
    }

    protected void ddlPayment_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            b = ddlPayment.SelectedValue.ToString();

            if (b == "1")
            {
                double cash = double.Parse(txtCash.Text);
                double total = double.Parse(txtTotal.Text);
                lblCash.Visible = true;
                txtCash.Visible = true;

                lblChange.Visible = true;
                txtChange.Visible = true;
                btnPay.Visible = false;
                if (cash > total)
                {
                    btnFinish.Visible = true;
                }
            }
            else
            {
                lblCash.Visible = false;
                txtCash.Visible = false;

                lblChange.Visible = false;
                txtChange.Visible = false;

                btnPay.Visible = true;
                btnFinish.Visible = false;
                txtCash.Text = "0";
                txtChange.Text = "0";
            }

        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }
    protected void btnFinish_Click(object sender, EventArgs e)
    {
        InsertToBill();
        LoadData();
        ReFresh();

    }
    protected void btnPay_Click(object sender, EventArgs e)
    {
        InsertToBill();
        LoadData();
        ReFresh();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            string id = GridView1.Rows[e.RowIndex].Cells[1].Text;

            if (product.deleteFromCart(Convert.ToInt32(id)) == 1)
            {
                LoadData();
            }
            else
            {
                Response.Write("delete fail!");
            }

            addMoneyToSub();
            MoneyWithTax();
            //if (lbltest.Visible == true)
            CheckChange();
        }
        catch (Exception EX)
        {
            EX.ToString();
        }
    }
    private void addMoneyToSub()
    {
        try
        {
            DataTable money = product.CartTotal();
            String TongTien = money.Rows[0]["TTTien"].ToString();
            txtSub.Text = TongTien;
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }
    private void loadProduct()
    {
        try
        {
            string a = ddlProTyp.SelectedValue.ToString();

            DataTable pro = product.getProName_ByPROTYP_ID(Convert.ToInt32(a));
            ddlPro.DataSource = pro;
            ddlPro.DataTextField = "PRO_NAME";
            ddlPro.DataValueField = "PRO_ID";
            ddlPro.DataBind();
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }
    private void LoadPayment()
    {
        try
        {
            PAYMENT_TYPEDataAPI payment = new PAYMENT_TYPEDataAPI();
            DataTable dt = payment.getPaymentType();
            ddlPayment.DataSource = dt;
            ddlPayment.DataTextField = "PTYP_NAME";
            ddlPayment.DataValueField = "PTYP_ID";
            ddlPayment.DataBind();
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }

    private void LoadData()
    {
        DataTable dt = product.getcartAmount();
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    private void MoneyWithTax()
    {

        try
        {
            double total;
            string SUb = txtSub.Text;
            if (SUb != "")
            {
                total = double.Parse(txtSub.Text) + (double.Parse(txtSub.Text) * (double.Parse(txtTax.Text) / 100));
            }
            else
                total = 0;
            txtTotal.Text = total.ToString();
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }
    private void CheckChange()
    {
        try
        {
            if (txtCash.Text == "")
                txtCash.Text = "0";

            double cash = double.Parse(txtCash.Text);
            double total = double.Parse(txtTotal.Text);
            if (cash >= total)
            {
                double Change = cash - total;
                txtChange.Text = Change.ToString();
                lblCashCheck.Text = "";
                btnFinish.Visible = true;
            }
            else if (cash != 0)
            {
                lblCashCheck.Text = "Your Cash must be greater or equal total ";
                lblCashCheck.Visible = true;
                txtCash.Text = "";
                btnFinish.Visible = false;
                txtChange.Text = "";
            }
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }
    private void ReFresh()
    {
        txtQuantity.Text = "";
        txtTax.Text = "";
        txtCash.Text = "";
        txtTotal.Text = "";
        txtSub.Text = "";
        txtChange.Text = "";
        txtCusName.Text = "";

        txtChange.Visible = false;
        txtTax.Visible = false;
        txtTotal.Visible = false;
        txtCash.Visible = false;

        btnFinish.Visible = false;
        btnPay.Visible = false;
        ddlPayment.Visible = false;


        lbl.Visible = false;
        lblCash.Visible = false;
        lblCashCheck.Visible = false;
        lblChange.Visible = false;
        lblPayment.Visible = false;
        lbltax.Visible = false;
        lbltest.Visible = false;
        lblTotal.Visible = false;
    }
    private void InsertToBill()
    {
        try
        { 
            BILLDataAPI BILL = new BILLDataAPI();
            //-----------old version---------
            //string CusName = txtCusName.Text;
            //double total = double.Parse(txtTotal.Text);
            //string Date = txtDate.Text;
            //string Paytyle = ddlPayment.SelectedItem.ToString();
                        
            ////-------- Moi Tao---------------
            //String Pro_name = ddlPro.SelectedItem.ToString();
            //int Quantity = int.Parse(txtQuantity.Text);

            //// >>dang use >>>BILL.InsertToBill(CusName, total, Date, Paytyle); 
            //-----------old version---------

            DataTable dt = product.getcartAmount();
            if (dt != null)
            {
                string name = txtCusName.Text;
                float total = 0;
                DateTime date = DateTime.Now;
                string paytype = ddlPayment.SelectedItem.ToString();
                for (int i = 0; i < dt.Rows.Count; i++)
                {                   
                    string proname = dt.Rows[i]["Product_Name"].ToString();
                    int quantity=int.Parse(dt.Rows[i]["quantity"].ToString());
                    float price = float.Parse(dt.Rows[i]["price"].ToString());
                    total = quantity * price;
<<<<<<< .mine
                    ////BILL.InsertAllToBill(name, total, date, paytype, proname, quantity);
=======
//                    BILL.InsertAllToBill(name, total, date, paytype, proname, quantity);
>>>>>>> .r293
                }
                
            }
            BILL.Delete_All_CART();
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }    
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.LoadData();
    }
}
